// import md5 from "md5";
// export { md5 };
/**
 * 10000 => 10,000.00
 * @param {number} number 数字
 * @param {number} fixed 小数点
 * @param {string} prefix 前缀
 * @param {string} seperator 分隔符
 * @param {string} decimal 小数点分隔符
 */
const formatNumber = (
  number = 0,
  fixed = 2,
  prefix = "¥",
  seperator = ",",
  decimal = "."
) => {
  const sign = 0 > number ? "-" : "";
  let k = parseInt((number = Math.abs(number || 0).toFixed(fixed)), 10) + "",
    g = 3 < (g = k.length) ? g % 3 : 0;
  return (
    prefix +
    sign +
    (g ? k.substr(0, g) + seperator : "") +
    k.substr(g).replace(/(\d{3})(?=\d)/g, "$1" + seperator) +
    (fixed
      ? decimal +
      Math.abs(number - k)
        .toFixed(fixed)
        .slice(2)
      : "")
  );
};
const formatTime = (timestamp, seperator = "-", seperator1 = ":") => {
  if (!timestamp) {
    timestamp = Date.now();
  }
  const date = new Date(parseInt(timestamp));

  let year = date.getFullYear();
  let month = (date.getMonth() + 1) < 10 ? `0${(date.getMonth() + 1)}` : (date.getMonth() + 1);
  let day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
  let hours = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours();
  let minutes = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
  let seconds = date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds();

  return (
    [year, month, day].join(seperator) + " " + [hours, minutes, seconds].join(seperator1)
  );
};
/** 获取随机字母加数字字符串 */
const randomString = length => {
  var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var sf = "";
  for (var i = 0; i < length; i++) {
    var Range = 62;
    var Rand = Math.random();
    var num = Math.floor(Rand * Range); //舍去
    sf += str.charAt(num);
  }
  return sf.toString();
};
/**
 * 全角转半角
 * 字符串全角转半角
 */
const transtaleToCDB = str => {
  var tmp = "";
  for (var i = 0; i < str.length; i++) {
    if (str.charCodeAt(i) > 65248 && str.charCodeAt(i) < 65375) {
      tmp += String.fromCharCode(str.charCodeAt(i) - 65248);
    } else {
      tmp += String.fromCharCode(str.charCodeAt(i));
    }
  }
  return tmp;
};
/**
 * 检测邮箱格式
 *
 * @param {string} email
 */
const checkEmailRules = (email) => {
  var reg = /^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  return reg.test(email)
}
/**
 * 检测是否是数字
 *
 * true为Number
 * @param {string} checkValue
 */
const checkNumber = (checkValue) => {
  let reg = /[^0-9.]/g;    //非数字和小数点`

  return !reg.test(checkValue)
}
/**
 * 检测是否是金额格式。
 * 只能包含一个小数点 & 小数点后两位
 * @param {string} checkValue
 * @returns {boolean}
 */
const checkAmount = (checkValue) => {
  let reg = /(^[1-9](\d+)*(\.\d{0,2})?$)|(^0$)|(^\d\.\d{0,2}$)/;
  return reg.test(checkValue)
}
/**
 *
 * @param phone
 * @param code
 * @returns {boolean}
 */
const checkPhone = (phone, code = "+86") => {
  /*中国大陆加港澳台手机正则验证
   中国大陆：开头1 3-9号段，后边跟9位数字
   台湾：09开头后面跟8位数字
   香港：9或6开头后面跟7位数字
   澳门：66或68开头后面跟5位数字*/
  var reg = "";
  switch (code){
    case "+86":
      reg = /^[1][3-9]\d{9}$/;
      break;
    case "+886":
      reg = /^[0]?[9]\d{8}$/;
      break;
    case "+852":
      reg = /^([6|9])\d{7}$|^[6]([8|6])\d{5}$/;
      break;
    default:
      reg = /^\d{8,}$/;
  }
  if (reg.test(phone)) {
    return true;
  } else {
    return false;
  }
}

/**
 *
 * @param password
 * @returns {boolean}
 */
const checkPassWord = (password) => {
  // 设置密码必须符合由数字,大写字母,小写字母,特殊符,至少其中三种组成密码
  // var reg = /^(?![A-Za-z]+$)(?![A-Z\\d]+$)(?![A-Z\\W]+$)(?![a-z\\d]+$)(?![a-z\\W]+$)(?![\\d\\W]+$)\\S{6,16}$/;
  // 设置密码必须符合由数字,大写字母,小写字母,特殊符,至少其中两种组成密码
  var reg = /^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S{6,16}$/;
  if (reg.test(password)) {
    return true;
  } else {
    return false;
  }
};
/**
 * 解析URL为对象
 * @param {String} url
 */
const parseQueryString = url => {
  var obj = {};
  var start = url.indexOf("?") + 1;
  var str = url.substr(start);
  var arr = str.split("&");
  for (var i = 0; i < arr.length; i++) {
    var arr2 = arr[i].split("=");
    obj[arr2[0]] = arr2[1];
  }
  return obj;
};
/** 复制粘贴板 */
const copyString = (text, cb) => {
  const input = document.createElement("input");
  input.setAttribute("readonly", "readonly");
  input.setAttribute("value", text);
  document.body.appendChild(input);
  input.setSelectionRange(0, input.value.length); //9999
  input.select();
  if (document.execCommand("copy")) {
    document.execCommand("copy");
    cb(0, "Copy success");
  } else {
    cb(-1, "Copy failed");
  }
  document.body.removeChild(input);
};
/** 获取对象类型 */
const toType = obj => {
  return {}.toString
    .call(obj)
    .match(/\s([a-zA-Z]+)/)[1]
    .toLowerCase();
};
/** 去除左右两端的空格 */
const trim = str => {
  if (str === "" && typeof str !== "string" && str.length === 0) return "";
  return str.replace(/(^\s*)|(\s*$)/g, "");
};

export {
  formatNumber,
  formatTime,
  randomString,
  transtaleToCDB,
  checkEmailRules,
  checkNumber,
  checkAmount,
  checkPhone,
  checkPassWord,
  parseQueryString,
  copyString,
  toType,
  trim,
};
